<script>
	import WebIM from '@/js_sdk/ring-letter/uni_sdk4.0.5.js'
	import configIm from '@/common/WebIMConfig.js'
	export default {
		globalData: {
			conn: {
				open: async opt => {
					WebIM.config = configIm;
					WebIM.conn = new WebIM.connection({
						appKey: WebIM.config.appkey,
						isMultiLoginSessions: WebIM.config.isMultiLoginSessions,
						https: false,
						url: WebIM.config.xmppURL,
				 	apiUrl: WebIM.config.apiURL,
						isAutoLogin: false,
						heartBeatWait: WebIM.config.heartBeatWait,
						autoReconnectNumMax: WebIM.config.autoReconnectNumMax,
						autoReconnectInterval: WebIM.config.autoReconnectInterval,
						isDebug: false,
				  isHttpDNS: false
					})

					return new Promise(async (call, err) => {
						try {

							// 登录开始
							let access = await WebIM.conn.open(opt);

							// 获取设备信息
							const res = uni.getSystemInfoSync();

							// 上传token
							let options = {
								deviceId: res.deviceId,
								deviceToken: access.accessToken,
								notifierName: configIm.appkey,
							}

							let result = await WebIM.conn.uploadToken(options);

							// 获取username
							let username = result.entities[0].username;

							// 修改用户信息
							// let userInfo = {
							//     nickname: '李宁龙',
							//     avatarurl: 'https://img1.baidu.com/it/u=4216761644,15569246&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
							//     mail: '123@qq.com',
							//     phone: '17633195905',
							//     gender: 'female',
							//     birth: '2000-01-01',
							//     sign: '南有乔木，不可休思。我写这句话的时候最想的就是你。',
							//     ext: ''
							// }

							// WebIM.conn.updateOwnUserInfo(userInfo).then((res) => {
							//     console.log(res)
							// })

							// 查询用户信息
							let info = await WebIM.conn.fetchUserInfoById(username);

							//返回用户信息
							call(info.data[username]);
						} catch (e) {
							console.error("error::: --->>", e);
							err(e);
						}
					})
				},
				close: () => {
					WebIM.conn.close();
				},
				listen: (callback) => {
					WebIM.conn.listen({
						onOpened: () => {
							console.log("连接成功");
							callback('onOpened')
						},
						onClosed: function() {
							console.log("连接失败");
							callback('onClosed')
						},
						onTextMessage: message => {
							console.log('收到文本消息')
							console.log(message)
							callback('onTextMessage', message)
						},
						onPresence: message => {
							console.log('处理“广播”或“发布-订阅”消息，如联系人订阅请求、处理群组、聊天室被踢解散等消息')
							console.log(message)
							callback('onPresence', message)
						},
						onOnline: message => {
							console.log('本机网络连接成功')
							console.log(message)
							callback('onOnline', message)
						},
						onOffline: message => {
							console.log('本机网络掉线')
							console.log(message)
							callback('onOffline', message)
						},
						onError: message => {
							console.log('失败回调')
							console.error("error::: --->>", message);
							callback('onError', message)
						},
						onRecallMessage: message => {
							console.log('收到撤回消息回调')
							console.log(message)
							callback('onRecallMessage', message)
						},
						onReceivedMessage: message => {
							console.log('收到消息送达服务器回执')
							console.log(message)
							callback('onReceivedMessage', message)
						},
				 	onDeliveredMessage: message => {
							console.log('收到消息送达客户端回执')
							console.log(message)
							callback('onDeliveredMessage', message)
						},
						onCreateGroup: message => {
							console.log('创建群组成功回执')
							console.log(this.groupid)
							callback('onCreateGroup', message)
						},
					});
				},
				webIm: () => {
					return {
						conn: WebIM.conn, // 客户端
						message: WebIM.message, // 消息
					}
				}
			},
		},
		sdk: {
			getMessageList() {
				WebIM.conn
			}
		}
	}
</script>

<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "@/uni_modules/uview-ui/index.scss";
</style>
